package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Resteraunt;
import com.wn.common.pojo.Rsp;
import com.wn.mapper.ResterauntMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class ResterauntService {

    @Autowired
    private ResterauntMapper resterauntMapper;

    //查询所有
    public PageInfo<Resteraunt> queryAll(PageParam<Resteraunt> pageParam){


        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<Resteraunt> gjlist = resterauntMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Resteraunt> ResterauntPageInfo = new PageInfo<Resteraunt>(gjlist);
            return ResterauntPageInfo;

        } else {

            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<Resteraunt> resteraunts = resterauntMapper.queryAll();
            PageInfo<Resteraunt> pageInfo = new PageInfo(resteraunts);
            return pageInfo;
        }


    }

    //逻辑删除
    public Rsp deletelogic(Resteraunt resteraunt){
        int key = resterauntMapper.deleteByPrimaryKey(resteraunt);
        if(key == 1){
            return new Rsp().success(key);
        }else{
            return new Rsp().fail().msg("删除失败");
        }
    }
    //根据id查询
    public Rsp selectById(Integer id){
        Resteraunt resteraunt = resterauntMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(resteraunt)){
            return new Rsp().fail().msg("查询失败");
        }else{
            return new Rsp().success(resteraunt);
        }
    }
    //增加数据
    public Rsp add(Resteraunt resteraunt){
        //根据添加的城市拿到id

        return resterauntMapper.insertSelective(resteraunt)==1?Rsp.success(null).msg("成功添加"):Rsp.fail().msg("添加失败");
    }


    //修改数据
    public Rsp update(Resteraunt resteraunt){
        return resterauntMapper.updateByPrimaryKeySelective(resteraunt)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败！");
    }

    //x修改状态
    public Rsp updateNewsinfo(Resteraunt resteraunt) {

        if(resteraunt.getStatus() == 1){
            resteraunt.setStatus(0);
        }else{
            resteraunt.setStatus(1);
        }

        int i = resterauntMapper.updateNewsinfo(resteraunt.getId(), resteraunt.getStatus());

        if (i== 1){
            return Rsp.success(null);
        }else{
            return Rsp.fail();
        }
    }
}
